package com.cqeec.waimai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqeec.waimai.common.CustomException;
import com.cqeec.waimai.dto.DishDto;
import com.cqeec.waimai.entity.Dish;
import com.cqeec.waimai.entity.DishFlavor;
import com.cqeec.waimai.mapper.DishMapper;
import com.cqeec.waimai.service.DishFlavorService;
import com.cqeec.waimai.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
        implements DishService{
    @Autowired
    DishFlavorService dishFlavorService;
    @Transactional //这里需要同时对付两张表，要么成功要么失败
    @Override
    public void saveWithFlavors(DishDto dishDto) {
        this.save(dishDto); //保存dish表相关的字段
        //保存dish_flavor相关的字段
        //获取页面传来的flavor数据
        List<DishFlavor> list = dishDto.getFlavors();
//        stream流的方式来给flavor中的dish_id赋值
        list=list.stream().map(item-> {
//            每一个item 相当于一次循环，dish_flavor表中一条数据
//            将dish表里的id 赋值给dish_flavor 表里的dish_id字段
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
//        批量保存
        dishFlavorService.saveBatch(list);
    }

    @Override
    public DishDto getByIdFlavor(Long id) {
//        根据id查询到dish表的数据
        Dish dish = this.getById(id);
//还需要准备口味表的数据
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
//        根据dish_flavor 表里的dish_id字段 查询
        wrapper.eq(DishFlavor::getDishId,dish.getId());
//        将查询到的相关口味数据添加到DishTdo对象中
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
//      修改id表的数据
        this.updateById(dishDto);
//       修改口味表的数据
//        1.删除原先的口味数据
//        根据dish_flavor表的dish来删除
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);
//        2.插入新的口味数据
        List<DishFlavor> list = dishDto.getFlavors();
        list.forEach(x->x.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(list);
    }

    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids!=null,Dish::getId,ids);
        List<Dish> list = this.list(queryWrapper);
        for(Dish dish:list){
            int status = dish.getStatus();
            if(status == 0){
                this.removeByIds(ids);
            }else {
                throw new CustomException("删除菜品中正在售卖菜品，无法删除");
            }
        }
    }
}
